package com.cf.studiomanage.controller;

import com.cf.studiomanage.entity.ClubApplication;
import com.cf.studiomanage.service.ClubApplicationService;
import com.cf.studiomanage.util.R;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/applications")
@Tag(name = "社团申请模块", description = "加入/退出社团申请相关接口")
public class ClubApplicationController {

    @Autowired
    private ClubApplicationService service;

    @PostMapping("/submit")
    @Operation(summary = "提交申请", description = "用户提交加入或退出社团申请")
    public R submit(@RequestBody ClubApplication application) {
        return service.submitApplication(application) ? R.ok("申请成功") : R.error("申请失败");
    }

    @GetMapping("/my/{userId}")
    @Operation(summary = "获取个人申请记录", description = "根据用户ID获取社团申请历史")
    public R getMyApplications(@PathVariable Long userId) {
        List<ClubApplication> list = service.getUserApplications(userId);
        return R.ok().data("list", list);
    }

    @GetMapping("/all")
    @Operation(summary = "管理员查看全部申请", description = "获取所有用户的申请记录")
    public R getAllApplications() {
        return R.ok().data("list", service.getAllApplications());
    }

    @PostMapping("/review")
    @Operation(summary = "管理员审核申请", description = "根据 ID 审批申请通过或拒绝")
    public R review(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        String status = map.get("status").toString();
        return service.reviewApplication(id, status) ? R.ok("审核成功") : R.error("审核失败");
    }
}
